//#include <head.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <signal.h>

#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/un.h>

#define SERVER_SOCKET "server.socket"
#define CLIENT_SOCKET "client.socket"
#define CLEAR_ARRAY(a) memset(a,0,sizeof(a))
#define CLEAR_MEM(addr,len) memset(addr,0,len)
static int handle_socket_read(int fd, const void *buf, size_t count);
static int handle_socket_write(int fd, const void *buf, size_t count);
static void init_signal_process(void);

void fn(char *p);
int main(int argc ,char** argv)
{
    int r=0;
    char buf[BUFSIZ];
    int fd =-1;
    struct sockaddr_un  myaddr, server_addr;
    socklen_t len;
    init_signal_process();
retry:
    fd = socket(AF_UNIX, SOCK_STREAM, 0);
    if(fd<0){
        printf("socket err:%s\n",strerror(errno));
        return -1;
    }
    
    unlink(CLIENT_SOCKET);
    memset(&myaddr,0,sizeof(myaddr));
    myaddr.sun_family =AF_UNIX;
    strcpy(myaddr.sun_path,CLIENT_SOCKET);
    len = offsetof(struct sockaddr_un, sun_path) + strlen(CLIENT_SOCKET);
    bind(fd,(struct sockaddr*)&myaddr,len);
    memset(&server_addr,0,sizeof(server_addr));
    server_addr.sun_family =AF_UNIX;
    strcpy(server_addr.sun_path,SERVER_SOCKET);
    len = offsetof(struct sockaddr_un, sun_path) + strlen(SERVER_SOCKET);
    

    while(1){
        r= connect(fd,(struct sockaddr*)&server_addr,len);
        if(r< 0){
            printf("connect err:%s\n",strerror(errno));
            sleep(1);
            close(fd);
            goto retry;

        }else if(r>0){
            printf("connect server ok!\n");
        }

        CLEAR_ARRAY(buf);
        //r= read(STDIN_FILENO,buf,sizeof(buf));
        //handle_socket_write(fd,buf,r);


        r= handle_socket_read(fd,buf,sizeof(buf));
        if(r== 0){
            printf("server close!\n");
            close(fd);
            sleep(1);
            goto retry;
        }else if(r<0){
            printf("read err:%s\n",strerror(errno));

        }else if(r>0){
            write(STDOUT_FILENO,buf,r);
        }
    }
    
    close(fd);
    return 0;
}
void fn(char *p)
{
    if(!p){
        return;
    }
    while (*p)
    {
        *p =toupper(*p);
        p++;
    }
    
}
static int handle_socket_write(int fd, const void *buf, size_t count)
{
    ssize_t r = write(fd, buf,  count);
    if(r== 0){
        printf("write 0 byte!!\n");
        close(fd);
    }else if(r<0){
        printf("write err:%s\n",strerror(errno));
    }else if(r>0){
        //write(STDOUT_FILENO,buf,r);
        printf("write %ld bytes.\n",r);
    }
    return r;
}
static int handle_socket_read(int fd, const void *buf, size_t count)
{
    ssize_t r = read(fd, buf,  count);
    if(r== 0){
        printf("server close!\n");
        close(fd);
    }else if(r<0){
        printf("read err:%s\n",strerror(errno));
    }else if(r>0){
        printf("read %ld  bytes.\n",r);
    }
    return r;
}
static void sig_fun(int sig)
{
    switch(sig){
    case SIGINT:
    case SIGTERM:	// kill -15 pid			
    case SIGSEGV:
    case SIGPIPE:
    //case SIGKILL:   // kill -9 pid   ϵͳĬ����Ϊ, ���ܸ���
       
        
        // sigprocess
        printf("--- sig %d\n",sig);
        //system_uninit();
        //_exit(1); //exit(1)
        break;
    //*	
    case SIGIO: 
        
        break;
    //*/	
    default:
        printf("--- sigX %d\n",sig);
        break;
    }
}
static void init_signal_process(void)
{
    signal(SIGPIPE,sig_fun);
}

